<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <div id="long">
      <h3>书架</h3>
      <ul>
        <li v-for="(item,index) in bookList">
          <span>{{ item.name }} </span>
          <!-- 注册点击事件,通过id删除元素 -->
          <button @click="DeltetBook(item.id)">删除</button>
        </li>
      </ul>
    </div>
    <script src="./vue.js"></script>
    <script>
      const long = new Vue({
        el: "#long",
        data: {
          bookList: [
            { id: 1, name: "<<红楼梦>>", author: "曹雪芹" },
            { id: 2, name: "<<西游记>>", author: "吴承恩" },
            { id: 3, name: "<<水浒传>>", author: "施耐庵" },
            { id: 4, name: "<<三国演义>>", author: "罗贯中" },
          ],
        },
        methods: {
          DeltetBook(bid) {
            // filter: 根据条件, 保留满足条件的对应项, 得到一个新数组
            this.bookList = this.bookList.filter((item) => item.id !== bid);
          },
        },
      });
    </script>
  </body>
</html>
